package com.lemon.secondhandplatform.ui.activity.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.Order;
import com.lemon.secondhandplatform.service.ShpService;
import com.lemon.secondhandplatform.ui.activity.BaseActivity;
import com.lemon.secondhandplatform.ui.adapter.OrderAdapter;
import com.lemon.secondhandplatform.ui.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.bmob.v3.listener.UpdateListener;

public class OrderActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private OrderAdapter adapter;
    private List<Order> orders;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        bindService();

        initView();
        bindListener();
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new OrderAdapter(this, orders);
        recyclerView.setAdapter(adapter);
    }

    private void bindListener() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        adapter.setOnOrderItemClickListener(new OrderAdapter.OnOrderItemClickListener() {
            @Override
            public void onCancelReserveClick(View v, final Order order) {
                binder.cancelReserve(order, new QueryListListener<BatchResult>() {
                    @Override
                    public void done(List<BatchResult> list, BmobException e) {
                        if (e == null) {
                            ToastUtils.show("成功取消订单");
                            orders.remove(order);
                            adapter.refresh(orders);
                        } else {
                            ToastUtils.show("网络不给力");
                        }
                    }
                });
            }

            @Override
            public void onCompleteTransactionClick(View v, final Order order) {
                order.setStatus(Order.BUSINESS_FINISHED);
                order.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            ToastUtils.show("交易完成");
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void afterBindService(ShpService.ShpBinder binder) {
        super.afterBindService(binder);
        binder.listOrders(orders == null ? 0 : orders.size(), new FindListener<Order>() {
            @Override
            public void done(List<Order> list, BmobException e) {
                if (e == null) {
                    orders = list;
                    adapter.refresh(orders);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUI();
    }

    private void updateUI() {
        if (binder != null) {
            binder.listOrders(orders == null ? 0 : orders.size(), new FindListener<Order>() {
                @Override
                public void done(List<Order> list, BmobException e) {
                    if (e == null) {
                        adapter.append(list);
                    }
                }
            });
        }
    }
}
